<?php

namespace App\Http\Controllers;

use App\Models\CourseModel;
use App\Models\SettingsModel;
use App\Services\CourseService;
use App\Services\SettingsService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class AboutController extends BaseController {

    /**
     * 基本设置
     */
    public function setting_index(Request $request) {
        $info = (new SettingsService())->info(3);
        $this->view_data['info'] = json_decode($info['content'], true);
        $this->view_data['info_type']  = 3;
        return view('about.setting_index', $this->view_data);
    }

    public function setting_save(Request $request)  {
        $phone = $request->post('phone', '');
        $fax = $request->post('fax', '');
        $email = $request->post('email', '');
        $address = $request->post('address', '');
        $name = $request->post('name', '');

        if (empty($phone) || empty($fax) || empty($email) || empty($address)){
            return $this->apiError('请填写信息');
        }

        $model = SettingsModel::find(3);
        $data = [
            "name" => $name,
            "phone" => $phone,
            "fax" => $fax,
            "email" =>  $email,
            "address" => $address,
        ];
        $model->content = json_encode($data);
        $model->save();

        return $this->apiSuccess('');
    }

    /**
     * 关于我们
     */
    public function about_index(Request $request) {
        $info = (new SettingsService())->info(1);
        $this->view_data['info'] = $info;
        $this->view_data['info_type']  = 1;
        return view('about.about_index', $this->view_data);
    }

    /**
     * 联系我们
     */
    public function contact_index(Request $request) {
        $info = (new SettingsService())->info(2);
        $this->view_data['info'] = $info;
        $this->view_data['info_type'] = 2;
        return view('about.about_index', $this->view_data);
    }

    public function about_save(Request $request) {
        $info_type = $request->post('info_type', 1);
        $info_id = $request->post('info_id', 1);
        $content = $request->post('content', '');
        $images = $request->post('images', '[]');
        if (empty($content)){
            return $this->apiError('请填写信息');
        }
        $info = (new SettingsService())->info($info_id);
        if (empty($info)){
            $model =  new SettingsModel();
        }else{
            $model = SettingsModel::find($info_id);
        }

        $model->info_type = $info_type;
        $model->content = $content;
        $model->images = json_decode($images, true);
        $model->save();
        return $this->apiSuccess('ok');
    }

    /**
     * 发展历程列表页面
     */
    public function course(Request $request) {
        return view('about.course_index', $this->view_data);
    }

    public function course_list(Request $request) {
        $params = $request->all();

        $list = (new CourseService())->list($params);

        return $this->apiSuccess($list);
    }

    public function course_create(Request $request) {
        $info_id = $request->get("info_id", 0);
        $info = [];
        if (!empty($info_id)){
            $info = (new CourseService())->info($info_id);
        }
        $this->view_data['info'] = $info;
        return view('about.course_create', $this->view_data);
    }

    public function course_save(Request $request) {
        $data = $request->all();

        $roules = [
            'title' => 'required|max:150',
            'date' => 'required',
            'content' => 'required',
            'images' => 'required|json',
        ];
        $message = [
            'title.required' => '请填写标题',
            'title.max' => '标题长度不能超过100个字',
            'date.date' => '请选择日期',
            'content.required' => '请填写内容',
            'images.required' => '请上传至少一张图片',
            'images.json' => '请上传至少一张图片',
        ];
        $validator = Validator::make($request->post(), $roules, $message);
        if ($validator->fails()) {
            $error_msg = $validator->errors()->first();
            return $this->apiError($error_msg, 1);
        }

        $info_id = data_get($data, 'info_id', 0);
        if (empty($info_id)) {
            $model = new CourseModel();
        }else{
            $model = CourseModel::find($info_id);
        }
        $model->title = data_get($data, 'title', '');
        $model->date = data_get($data, 'date', '');
        $model->content = data_get($data, 'content', '');
        $model->images = json_decode(data_get($data, 'images', ''));
        $model->save();

        if (!$model->id) {
            return $this->apiError('请填写信息');
        }
        return $this->apiSuccess('ok');
    }

    public function course_delete(Request $request) {
        $info_id = $request->get("info_id", 0);
        (new CourseService())->delete($info_id);
        return $this->apiSuccess('ok');
    }
}